package com.example.memorycardgame;

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class EditInfoActivity extends AppCompatActivity {
    private EditText etName, etGender, etPhone;
    private Button btnSave;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_info);

        // 初始化控件
        initViews();
        // 接收并显示当前信息
        showCurrentInfo();
        // 绑定保存按钮事件
        bindSaveEvent();
    }

    private void initViews() {
        etName = findViewById(R.id.et_name);
        etGender = findViewById(R.id.et_gender);
        etPhone = findViewById(R.id.et_phone);
        btnSave = findViewById(R.id.btn_save);
    }

    // 显示从主界面传递过来的当前信息
    private void showCurrentInfo() {
        Intent intent = getIntent();
        if (intent != null) {
            String name = intent.getStringExtra("name");
            String gender = intent.getStringExtra("gender");
            String phone = intent.getStringExtra("phone");

            etName.setText(name);
            etGender.setText(gender);
            etPhone.setText(phone);
        }
    }

    private void bindSaveEvent() {
        btnSave.setOnClickListener(v -> {
            // 获取输入的新信息
            String newName = etName.getText().toString().trim();
            String newGender = etGender.getText().toString().trim();
            String newPhone = etPhone.getText().toString().trim();

            // 简单校验（非空）
            if (newName.isEmpty()) {
                etName.setError("姓名不能为空");
                return;
            }

            // 返回结果给主界面
            Intent resultIntent = new Intent();
            resultIntent.putExtra("name", newName);
            resultIntent.putExtra("gender", newGender);
            resultIntent.putExtra("phone", newPhone);
            setResult(RESULT_OK, resultIntent);

            // 关闭当前界面
            finish();
        });
    }
}